package com.evolution.warlock.excle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import com.evolution.warlock.entity.CivilDispute;
import com.evolution.warlock.entity.CriminalBase;
import com.evolution.warlock.entity.CriminalMember;
import com.evolution.warlock.entity.RobSteal;

public class ExcelConfiguration {
	
	public ExcelConfiguration() {
		// TODO Auto-generated constructor stub
		try {
			setFormat();
		} catch (NoSuchFieldException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		
		// for test
//		setHeaderMap(CriminalBase.class, "temporaryGuy", "是否为流动人口", null);
//		setHeaderMap(CriminalBase.class, "temporaryRegist", "是否暂住登记", null);
//		setHeaderMap(CriminalBase.class, "temporaryNoRegistDura", "未暂住登记的时间段", null);
	}
	
	/**
	 * 静态化设置
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 * 
	 * */
	private void setFormat() throws NoSuchFieldException, SecurityException{
		
		//静态映射值： 类Class, 类中对应的属性名称（用于反射）, 标题，子标题。  
		//硬编码配置，需要每次修改。
		
		//CriminalBase 
		setHeaderMap(CriminalBase.class, "temporaryGuy", "是否为流动人口", null);
		setHeaderMap(CriminalBase.class, "temporaryRegist", "是否暂住登记", null);
		setHeaderMap(CriminalBase.class, "temporaryNoRegistDura", "未暂住登记的时间段", null);
		setHeaderMap(CriminalBase.class, "temporaryFixAd", "是否有固定住所", null);
		setHeaderMap(CriminalBase.class, "temporaryFixJob", "是否有固定工作", null);
		setHeaderMap(CriminalBase.class, "temporaryTransTools", "乘坐何种交通工具来本地", null);
		setHeaderMap(CriminalBase.class, "beforeArrestDays", "被抓前在案发所在地区停留时间", null);
		setHeaderMap(CriminalBase.class, "localAreaCategory", "本地落脚点类型", null);
		setHeaderMap(CriminalBase.class, "localCriminalArea", "本地落脚点地区", null);
		setHeaderMap(CriminalBase.class, "localCriminalAddress", "本地落脚点地址", null);
		setHeaderMap(CriminalBase.class, "localCriminalPoliceName", "本地落脚点所属派出所", null);
		setHeaderMap(CriminalBase.class, "ciminalMatesNum", "作案同伙人数", null);
		setHeaderMap(CriminalBase.class, "ciminalNum", "作案次数", null);
		
		//CriminalBase ->CriminalMember    对象 与  展示标题的 二级 映射关系，静态化：字段改变需要手动修改代码添加。
		setHeaderMap(CriminalMember.class,"idCardNum","身份证号",null);
		setHeaderMap(CriminalMember.class,"gender","性别 ",null);
		setHeaderMap(CriminalMember.class,"name","姓名",null);
		setHeaderMap(CriminalMember.class,"alias","绰号\\代号\\曾用名",null);
		setHeaderMap(CriminalMember.class,"age","年龄",null);
		setHeaderMap(CriminalMember.class,"marriage","婚姻状况",null);
		setHeaderMap(CriminalMember.class,"profession","职业",null);
		setHeaderMap(CriminalMember.class,"parentDivorce","父母有无离异经历",null);
		setHeaderMap(CriminalMember.class,"religion","宗教",null);
		setHeaderMap(CriminalMember.class,"ethnic","民族",null);
		setHeaderMap(CriminalMember.class,"educationLevel","文化程度",null);
		setHeaderMap(CriminalMember.class,"sonOfFarmer","是否为农民工子女",null);
		setHeaderMap(CriminalMember.class,"criminalBefore","违法犯罪前科的情况",null);
		setHeaderMap(CriminalMember.class,"phoneNumber","联系方式","手机");
		setHeaderMap(CriminalMember.class,"telephoneNumber","联系方式","座机");
		setHeaderMap(CriminalMember.class,"mainIncome","收入主要来源 ",null);
		setHeaderMap(CriminalMember.class,"noRigisterCensus","是否为黑户",null);
		setHeaderMap(CriminalMember.class,"householdArea","户籍地址","地区");
		setHeaderMap(CriminalMember.class,"householdAddress","户籍地址","住址");
		setHeaderMap(CriminalMember.class,"householdPoliceStationName","户籍地址","所属派出所名称");
		setHeaderMap(CriminalMember.class,"currentArea","现住址","地区");
		setHeaderMap(CriminalMember.class,"currentAddress","现住址","住址");
		setHeaderMap(CriminalMember.class,"currentPoliceStationName","现住址","所属派出所名称");
		//addRowHeader(CriminalMember.class,"","涉案类别",null);
		setHeaderMap(CriminalMember.class,"relativeName","亲属姓名",null);
		setHeaderMap(CriminalMember.class,"relativeArea","亲属居住地","亲属居住地区");
		setHeaderMap(CriminalMember.class,"relativeAddress","亲属居住地","亲属居住地址");
		setHeaderMap(CriminalMember.class,"relativeAddressPoliceName","亲属居住地","亲属居住地所属派出所");
		
		
		//CriminalBase ->RobSteal robSteal  刑事犯罪记录号
		//3.1.3	“两抢一盗”违法犯罪嫌疑人作案信息采集表
		setHeaderMap(RobSteal.class, "criminalMainReason", "作案主要原因", null);
	//	setHeaderMap(RobSteal.class, "criminalMainReasonAnother", "作案主要原因其它", null);
		setHeaderMap(RobSteal.class, "localCriminalReason", "本地作案主要原因", null);
	//	setHeaderMap(RobSteal.class, "localCriminalReasonAnother", "本地作案主要原因其它", null);
		setHeaderMap(RobSteal.class, "criminalDate", "作案时间", null);
		setHeaderMap(RobSteal.class, "criminalDateReason", "选择该时间段的原因 ", null);
		setHeaderMap(RobSteal.class, "criminalAreaChoice", "作案主要选择的区域", null);
	//	setHeaderMap(RobSteal.class, "criminalAreaChoiceAnother", "作案主要选择的区域其它", null);
		setHeaderMap(RobSteal.class, "criminalAreaChoiceReason", "该区域作案的主要原因", null);
		setHeaderMap(RobSteal.class, "robCategory",  "犯案类型","抢劫");
		setHeaderMap(RobSteal.class, "snatchCategory", "犯案类型", "抢夺");
		setHeaderMap(RobSteal.class, "theftCategory", "犯案类型", "盗窃");
		setHeaderMap(RobSteal.class, "sharpCategory", "犯案类型", "诈骗");
		
		setHeaderMap(RobSteal.class, "transportationTools", "作案交通工具选择", null);
		setHeaderMap(RobSteal.class, "transportationToolSource", "作案交通工具来源", null);
		//如果选择黑车租赁公司，增加公司的名称和地址输入框	
		setHeaderMap(RobSteal.class, "rentCarCorpName", "租赁公司的名称", null);
		setHeaderMap(RobSteal.class, "rentCarCorpAddr", "租赁公司的地址", null);
		setHeaderMap(RobSteal.class, "driveNumberBool", "有无驾照", null);
		
		setHeaderMap(RobSteal.class, "leaveCriminalSceneCategory", "作案后逃离现场的选择", null);
		setHeaderMap(RobSteal.class, "leaveCriminalSceneToolsCategory", "作案后逃离现场的工具选择", null);
		setHeaderMap(RobSteal.class, "leaveCriminalSceneToolsSourceCategory", "作案后逃离现场的工具来源选择", null);
		
		setHeaderMap(RobSteal.class, "addressAfterCrimeCategory", "作案后藏匿本地的情况", null);
		setHeaderMap(RobSteal.class, "addressAfterCrimeArea", "作案后藏匿地区", null);
		setHeaderMap(RobSteal.class, "addressAfterCrimeAddress", "作案后藏匿地址", null);
		
		setHeaderMap(RobSteal.class, "bootySellCondition", "赃物销赃情况", null);
		setHeaderMap(RobSteal.class, "bootySellAreaCategory", "销赃地区选项", null);
		setHeaderMap(RobSteal.class, "bootySellOption", "销赃方式", null);
		setHeaderMap(RobSteal.class, "illegalMoney", "赃款去向", null);
		
		//TODO ::: 格式
		setHeaderMap(RobSteal.class, "gamblingWhoringDrugCategory", "涉及黄赌毒违法经历情况 ", null);
		setHeaderMap(RobSteal.class, "gamblingArea", "赌博常去的地区", null);
		setHeaderMap(RobSteal.class, "gamblingAddress", "赌博常去的地址", null);
		setHeaderMap(RobSteal.class, "whoringArea", "嫖娼常去de地区", null);
		setHeaderMap(RobSteal.class, "whoringAddress", "嫖娼常去的地址", null);
		setHeaderMap(RobSteal.class, "drugArea", "吸毒常去得地区", null);
		setHeaderMap(RobSteal.class, "drugAddress", "吸毒常去得地址", null);
		
		setHeaderMap(RobSteal.class, "frequentedAccessHotelStay", "本地落脚点酒店情况 ", null);
		setHeaderMap(RobSteal.class, "frequentedAccessHotelName", "常去宾馆名 ", null);
		setHeaderMap(RobSteal.class, "frequentedAccessHotelLevel", "宾馆档次", null);
		setHeaderMap(RobSteal.class, "hotelNoRegisterName", "不登记可入住的旅馆酒店名称", null);
		setHeaderMap(RobSteal.class, "hotelNoRegisterAddress", "没有登记就可以入住的旅馆地址", null);
		
		setHeaderMap(RobSteal.class, "favoriteAmusementOption", "本地常去娱乐消费场所", "场所类型");
		setHeaderMap(RobSteal.class, "favoriteAmusementArea", "本地常去娱乐消费场所", "所在地区");
		setHeaderMap(RobSteal.class, "favoriteAmusementAddress", "本地常去娱乐消费场所", "场所地址");
		
		setHeaderMap(RobSteal.class, "frequentedRestaurantArea", "宵夜的地区", null);
		setHeaderMap(RobSteal.class, "frequentedRestaurantAddress", "宵夜常去的选择", null);
		setHeaderMap(RobSteal.class, "frequentedRestaurantName", "宵夜常去的店名", null);
		
		setHeaderMap(RobSteal.class, "internetOption", "上网情况", "是否上网");
		setHeaderMap(RobSteal.class, "internetDetails", "上网情况", "上网备注");
		setHeaderMap(RobSteal.class, "internetUserName", "上网情况", "上网帐号");
		setHeaderMap(RobSteal.class, "internetPassword", "上网情况", "上网密码");
		
		setHeaderMap(RobSteal.class, "fakeIdCardOption", "是否使用假身份证", null);// TODO ： ？？
		setHeaderMap(RobSteal.class, "fakeIdCardDetail", "是否使用假身份证以及详情", null); 
		setHeaderMap(RobSteal.class, "fakeIdCardArea", "假证件办理地点", "地区");
		setHeaderMap(RobSteal.class, "fakeIdCardAddress", "假证件办理地点", "地址");
		
		setHeaderMap(RobSteal.class, "fraudulentCardOption", "使用冒用他人身份证", null);
		setHeaderMap(RobSteal.class, "fraudulentCardDetails", "使用冒用他人身份证以及详情", null);
		
		setHeaderMap(RobSteal.class, "censusOption", "户籍类", null); //TODO ???到本地公安办理业务的情况
		setHeaderMap(RobSteal.class, "censusDepartmentName", "户籍类单位名", null);
		setHeaderMap(RobSteal.class, "censusDetails", "户籍类详情", null);
		setHeaderMap(RobSteal.class, "temporaryDepartmentName", "暂住类单位名", null);
		setHeaderMap(RobSteal.class, "temporaryDetails", "暂住类详情", null);
		setHeaderMap(RobSteal.class, "trafficOption", "交通类", null);
		setHeaderMap(RobSteal.class, "trafficDepartmentName", "交通类单位名", null);
		setHeaderMap(RobSteal.class, "trafficDetails", "交通详情", null);
		setHeaderMap(RobSteal.class, "fireFightingDepartmentName", "消防类单位名", null);
		setHeaderMap(RobSteal.class, "fireFightingDetails", "消防类详情", null);
		setHeaderMap(RobSteal.class, "reportOption", "报案类", null);
		setHeaderMap(RobSteal.class, "reportDepartmentName", "报案类单位名", null);
		setHeaderMap(RobSteal.class, "reportDetails", "报案详情", null);
		//TODO : 三所地址   三所部门名 ?????
		setHeaderMap(RobSteal.class, "thdOfficeDepartment1", "三所部门名1", null);
		setHeaderMap(RobSteal.class, "thdOfficeDepartmentDetail1", "三所地址1 ??", null);
		setHeaderMap(RobSteal.class, "thdOfficeDepartment2", "三所部门名2", null);
		setHeaderMap(RobSteal.class, "thdOfficeDepartmentDetail2", "三所地址2 ??", null);
		setHeaderMap(RobSteal.class, "thdOfficeDepartment3", "三所部门名3", null);
		setHeaderMap(RobSteal.class, "thdOfficeDepartmentDetail3", "三所地址3 ??", null);
		setHeaderMap(RobSteal.class, "anotherDepartmentName", "其他业务单位名", null);
		setHeaderMap(RobSteal.class, "anotherOptionDetails", "其他业务详细", null);
		
		setHeaderMap(RobSteal.class, "contents", "备注", null);
		
		
		//CriminalBase ->CivilDispute: civilDispute  行政犯罪记录号
		setHeaderMap(CivilDispute.class, "caseCategoryInjure", "涉案类别--故意伤害案", null); //TODO ???
		setHeaderMap(CivilDispute.class, "caseCatetoryKill", "涉案类别--故意杀人案", null);	//TODO ???
		setHeaderMap(CivilDispute.class, "civilDisputeCategory", "民事纠纷类别", null);
		setHeaderMap(CivilDispute.class, "civilDisputeCondition", "民事纠纷调处情况", null);
		setHeaderMap(CivilDispute.class, "civilDisputeResult", "民事纠纷调处结果", null);
		setHeaderMap(CivilDispute.class, "civilDisputeTimeStamp", "民事纠纷发生至刑案发生时间间隔", null);
		setHeaderMap(CivilDispute.class, "caseCategory", "作案类型", null);
		setHeaderMap(CivilDispute.class, "mainReason", "主要成因", null);
//		setHeaderMap(CivilDispute.class, "", "", null);
//		setHeaderMap(CivilDispute.class, "", "", null);
	}
	

	private void setHeaderMap(Class class1,String attr, String parentItemName, String childItemName) throws NoSuchFieldException, SecurityException {
		// TODO Auto-generated method stub
		if(parentItemName == null || parentItemName.trim().equals("")){
			throw new IllegalArgumentException("当class="+class1+" and attr="+attr+"时，参数parentItemName不能为空！");
		}
		
		if(headers.containsKey(parentItemName)){
			if(childItemName == null){
				throw new IllegalArgumentException("当class="+class1+" and attr="+attr+"时，参数parentItemName="+parentItemName+" 有重复项！");
			}else{
				List<String> children = headers.get(parentItemName);
				if(children == null){
					throw new IllegalArgumentException("当class="+class1+" and attr="+attr+"时，参数parentItemName="+parentItemName+" 同时存在一级和二级配置！");
				}else{
					children.add(childItemName);
				}
			}
		}else{
			if(childItemName == null){
				headers.put(parentItemName, null);
			}else{
				List<String> children = new ArrayList<String>();
				children.add(childItemName);
				headers.put(parentItemName, children);
			}
			
			parentItems.add(parentItemName);
		}
		
		AttributiesBean bean = new AttributiesBean(class1, attr);
		attributies.add(bean);
		
	}
	
	
	//标题：
	//二级标题：Map维护数据。
	private Map<String,List<String>> headers = new HashMap<String,List<String>>();
	//维护顺序
	private List<String> parentItems = new ArrayList<String>();
	
	//
	private List<AttributiesBean> attributies = new ArrayList<AttributiesBean>();
	
	
	

	
	private static HashMap<Class,List<String>> attributes = new HashMap<Class,List<String>>();


	public void renderHeader(HSSFWorkbook wb, HSSFSheet st, HSSFCellStyle normalStyle) {
		int k=0;
		Row row0 = st.createRow(0);
		Row row1 = st.createRow(1);
		for(String parentItem: parentItems){
			//System.out.println(parentItem);
			List<String> children = headers.get(parentItem);
			if(children==null){
				st.addMergedRegion(new CellRangeAddress(0, 1, k, k));
				Cell cell= row0.createCell(k);
				cell.setCellValue(parentItem);
				cell.setCellStyle(normalStyle);
				
				row1.createCell(k).setCellStyle(normalStyle);
				
				k=k+1;
			}else{
				int colSpan = children.size();
				
				int lastCol = k+colSpan-1;
				st.addMergedRegion(new CellRangeAddress(0, 0, k, lastCol));
				Cell cell= row0.createCell(k);
				cell.setCellValue(parentItem);
				cell.setCellStyle(normalStyle);
				int i = k+1;
				while(i <= lastCol){
					row0.createCell(i).setCellStyle(normalStyle);
					i++;
				}
				

				int j=0;
				for(String child : children){
					Cell childCell= row1.createCell(k+j);
					childCell.setCellStyle(normalStyle);
					childCell.setCellValue(child);

					j++;
				}
				
				k = lastCol + 1;
			}
		}
	}

	public void renderDatas(HSSFWorkbook wb, HSSFSheet st, HSSFCellStyle normalStyle, List<CriminalBase> list) throws IllegalArgumentException, IllegalAccessException {
		int rowIndex = 2;
		for (CriminalBase criminalBase : list) {
			renderOneRow(st, normalStyle, rowIndex, criminalBase);
		}
	}

	private void renderOneRow(HSSFSheet st, HSSFCellStyle normalStyle, int rowIndex, CriminalBase criminalBase)
			throws IllegalAccessException {
		CriminalMember criminalMember= criminalBase.getCriminalNumber();
		RobSteal robSteal = criminalBase.getRobSteal();
		CivilDispute civilDispute = criminalBase.getCivilDispute();
		
		//int rowPos=rowIndex;
		Row row = st.createRow(rowIndex);
		int colPos = 0;
		for(AttributiesBean bean : attributies) {
			Object objValue;
			if(CriminalBase.class.getSimpleName().equals(bean.getClassName())){
				objValue = bean.get(criminalBase);
			}else if(CriminalMember.class.getSimpleName().equals(bean.getClassName())){
				objValue = bean.get(criminalMember);
			}else if(RobSteal.class.getSimpleName().equals(bean.getClassName())){
				objValue = bean.get(robSteal);
			}else if(CivilDispute.class.getSimpleName().equals(bean.getClassName())){
				objValue = bean.get(civilDispute);
			}else{
				throw new IllegalArgumentException("新增类"+bean.getClassName()+"没有实现处理，需要添加处理该类的代码！");
			}
			
			
			Cell cell= row.createCell(colPos);
			//FIXME :: 
			if(objValue!=null){
				cell.setCellValue(objValue.toString());
			}
			cell.setCellStyle(normalStyle);
			colPos++;
			
		}
	}

	private void renderOneRow(CriminalBase criminalBase, int rowIndex) throws IllegalArgumentException, IllegalAccessException {

	}


		
}
